package com.ifast.common.aspect;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.ifast.api.util.JWTUtil;
import com.ifast.common.utils.Const;
import com.ifast.integral.service.IntegralFromService;
import com.ifast.sys.domain.UserDO;
import com.ifast.sys.service.UserService;

/**
 * <pre>
 *  积分切面
 * </pre>
 * <small> 2018年3月22日 | ShiQiang</small>
 */
@Aspect
@Component
public class IntegralAspect {
	
	@Autowired
    private IntegralFromService integralFromService;
	@Autowired
	private UserService userService;
   
	/**
	 * 登录积分
	 */ 
    @After(value="execution(* com.ifast.sys.service.UserService.getUserInfoForApp(..)) && args(token)",argNames="token")
    public void loginScore(String token) throws Exception {  
        UserDO  currUser =  userService.selectById(JWTUtil.getUserId(token));
        if(null != currUser){
        	integralFromService.obtainAspectIntegral(currUser.getId().toString(), currUser.getDwCode(), Const.integral.integral_login);
        } 
    }
    
    /**
     * 照片上传获取积分  
     */ 
    @After(value="execution(* com.ifast.album.service.AlbumPhotoService.upload(..))&& args(userId,dwCode,albumId,msg,request)",argNames="userId,dwCode,albumId,msg,request")
    public void uploadScore(String userId,String dwCode,String albumId,String msg, HttpServletRequest request) throws Throwable {  
    	UserDO  currUser =  userService.selectById(userId);
        if(null != currUser) {
        	integralFromService.obtainAspectIntegral(currUser.getId().toString(), currUser.getDwCode(), Const.integral.integral_photo);
        } 
    }
    
    /**
     * 考试获取积分
     */ 
//    @Around(value="execution(* com.ifast.integral.service.IntegralExamScoreService.integral(..)) && args(type,userId,businessId,isRight)",argNames="type,userId,businessId,isRight")
//    public void versionScore(Const.integral type,String userId,String businessId,boolean isRight) throws Throwable { 
//    	UserDO  currUser =  userService.selectById(userId);
//        if(null != currUser) {
//        	integralFromService.obtainIntegral(currUser.getId().toString(), currUser.getDwCode(),type,businessId,isRight);
//        } 
//    }
}
